//
//  AppDelegate.m
//  NewApp
//
//  Created by 健 吕 on 12-6-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"
#import "HomeViewController.h"
#import "ProductListViewController.h"
#import "ShoppingCarViewController.h"
#import "MemberShipViewController.h"
#import "NavigatorViewController.h"
#import "UINavigationBar+Additions.h"
#import "LoginViewController.h"
#import "BasePageViewController.h"
#import "Constants.h"
#import "DigitNoteViewController.h"


ImageDownLoadMgr *g_imageLoader = nil;


@implementation AppDelegate

@synthesize window = _window;
@synthesize tabBarController = _tabBarController;

- (void)dealloc
{
    [_window release];
    [_tabBarController release];
    [super dealloc];
}

- (void) splashDidLoadCompleted {
    
    HomeViewController *homeViewController = [[HomeViewController alloc] init];
    homeViewController.title = @"返回";
    homeViewController.tabBarItem.title = @"首頁";
    homeViewController.tabBarItem.image = [UIImage imageNamed:@"first_page_icon.png"];
    
    DigitNoteViewController *digitNoteViewController = [[DigitNoteViewController alloc] init];
    digitNoteViewController.title = @"返回";
    digitNoteViewController.tabBarItem.title = @"電子型錄";
    digitNoteViewController.tabBarItem.image = [UIImage imageNamed:@"product_list_icon.png"];
    
    ShoppingCarViewController *shoppingCarViewController = [[ShoppingCarViewController alloc] init];
    shoppingCarViewController.title = @"返回";
    shoppingCarViewController.tabBarItem.title = @"購物車";
    shoppingCarViewController.pageType = PAGE_TYPE_SHOP;
    shoppingCarViewController.tabBarItem.image = [UIImage imageNamed:@"shopping_trolley_icon.png"];
    
    BasePageViewController *membershipViewController = [[MemberShipViewController alloc] init];

    //MemberShipViewController *membershipViewController = [[MemberShipViewController alloc] init];
    membershipViewController.title = @"返回";
    membershipViewController.tabBarItem.title = @"會員";
    membershipViewController.tabBarItem.image = [UIImage imageNamed:@"member_icon.png"];
    
    
    NavigatorViewController *navigatorViewController = [[NavigatorViewController alloc] init];
    navigatorViewController.title = @"返回";
    navigatorViewController.tabBarItem.title = @"導覽";
    navigatorViewController.tabBarItem.image = [UIImage imageNamed:@"guide_icon.png"];
    
    UINavigationController *navitaionHome = [[UINavigationController alloc] initWithRootViewController:homeViewController];
    [navitaionHome.navigationBar setBkImage:[UIImage imageNamed:@"top_logo.png"]];
    [homeViewController release];
    
    UINavigationController *navitaionProductList = [[UINavigationController alloc] initWithRootViewController:digitNoteViewController];
    [navitaionProductList.navigationBar setBkImage:[UIImage imageNamed:@"top_logo.png"]];
    [digitNoteViewController release];
    
    UINavigationController *navitaionShoppingCar = [[UINavigationController alloc] initWithRootViewController:shoppingCarViewController];
    [navitaionShoppingCar.navigationBar setBkImage:[UIImage imageNamed:@"top_logo.png"]];
    [shoppingCarViewController release];

    UINavigationController *navitaionMembership = [[UINavigationController alloc] initWithRootViewController:membershipViewController];
    [navitaionMembership.navigationBar setBkImage:[UIImage imageNamed:@"top_logo.png"]];
    [membershipViewController release];
    
    UINavigationController *navigationGuide = [[UINavigationController alloc] initWithRootViewController:navigatorViewController];
    [navigationGuide.navigationBar setBkImage:[UIImage imageNamed:@"top_logo.png"]];
    [navigatorViewController release];
    

    
    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:navitaionHome,navitaionProductList,navitaionShoppingCar,navitaionMembership,navigationGuide, nil];
    
    self.window.rootViewController  = self.tabBarController;
    [self.window makeKeyAndVisible];
    
    [navitaionHome release];
    [navitaionProductList release];
    [navitaionShoppingCar release];
    [navitaionMembership release];
    [navigationGuide release];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    //Initialize image downloading instance
    g_imageLoader = [[ImageDownLoadMgr alloc] initWithCachedCapacity:50];
    
    SplashViewController *viewCtrl = [[SplashViewController alloc] init];
    viewCtrl.delegate = self;
    
    self.window.rootViewController = viewCtrl;
    [viewCtrl release];
    [self.window makeKeyAndVisible];
    return YES;
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

+ (AppDelegate *) theAppDelegate {
    return (AppDelegate *)[UIApplication sharedApplication].delegate;
}

@end
